package com.iwith.dongmoon.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.iwith.dongmoon.gsonlist.PictureList;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.ImageDownloader;
import com.iwith.dongmoon.util.ImageDownloader.Mode;
import com.iwith.dongmoon.util.RequestData;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class EditPhotoActivity extends Activity implements OnClickListener {
	private ImageView photo;
	private Button deleteBt;
	private Button prevBt;
	
	private ImageView title;

	private AlertDialog.Builder mAlertDialog;
	
	private String code;
	private String imagepath;
	private Integer wr_id;
	private String userid;
	
	private ImageDownloader mImageDownloader;;
	private Context mContext;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.editphoto);
	    // TODO Auto-generated method stub
	}
	
	@Override
	public void onResume(){
		super.onResume();
		ThreadTask.onFinishAll();
		Initialize();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		ThreadTask.onFinishAll();
	}
	
	private void Initialize(){
		mContext = this;
		
		userid = getIntent().getExtras().getString("userid");
		wr_id = getIntent().getExtras().getInt("wr_id");
		code = getIntent().getExtras().getString("code");
		imagepath = getIntent().getExtras().getString("imagepath");
		
		mImageDownloader = new ImageDownloader();
		mImageDownloader.setMode(Mode.CORRECT);
		
		title=(ImageView)findViewById(R.id.title);
		photo=(ImageView)findViewById(R.id.editphoto_photo);
		
		deleteBt=(Button)findViewById(R.id.editphoto_deleteBt);
		prevBt=(Button)findViewById(R.id.backbtn);
		title = (ImageView) findViewById(R.id.title);
		
		mAlertDialog = new AlertDialog.Builder(this);
		
		ThreadInterface threadInterface=new ThreadInterface(){
			public void onThread() {
			}

			public void onThreadEnd() {
				if(imagepath!=null)
					mImageDownloader.download(imagepath, photo);
			}
		};
		
		ThreadTask task = new ThreadTask(this,	threadInterface);
		task.execute();
		
		if(code.equals(ApiCode.USERPHOTO)==true || code.equals(ApiCode.BIZPHOTO)==true){
			deleteBt.setVisibility(View.GONE);
		} else if(code.equals(ApiCode.MODIFYUSERPHOTO)==true || code.equals(ApiCode.MODIFYBIZPHOTO)==true){
			deleteBt.setVisibility(View.VISIBLE);
		}
				
		deleteBt.setOnClickListener(this);
		title.setOnClickListener(this);
		prevBt.setOnClickListener(this);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==title.getId()){
			Intent intent=new Intent(this, MainIconView.class);
			intent.putExtra("flag", true);
			startActivity(intent);
		} else if(v.getId()==prevBt.getId()){
			finish();
		} else if(v.getId()==deleteBt.getId()){
			mAlertDialog.setMessage("정말 삭제하시겠습니까?");
			mAlertDialog.setPositiveButton("삭제", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					String api=null;
					if(code.equals(ApiCode.MODIFYUSERPHOTO)==true){
						api = ApiCode.API_DELETEPICTURE;
					} else if(code.equals(ApiCode.MODIFYBIZPHOTO)==true){
						api = ApiCode.API_DELETEBIZPICTURE;
					}
					Gson gson = new Gson();
					RequestData data = new RequestData(mContext, api, "", "");
					data.addParameter("userid", userid);
					data.addParameter("wr_id", wr_id.toString());
					String sresult = data.getDataPost();
					PictureList pictureList = gson.fromJson(sresult, PictureList.class);
					if(pictureList!=null){
						if(pictureList.getResultCode().equals(ResultCode.SUCCESS)==true){
							finish();
						} else {
							Toast.makeText(mContext, "사진 삭제 실패 : "+pictureList.getResultMessage(), Toast.LENGTH_LONG).show();
						}
					} else {
						Toast.makeText(mContext, "사진 삭제 실패 : 잘못된 구문", Toast.LENGTH_LONG).show();
					}
				}
			});
			mAlertDialog.setNegativeButton("취소", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			mAlertDialog.show();
		}
	}
}
